#include "filebrowserengine.h"

FileBrowserEngine::FileBrowserEngine()
{
    p_engineServer = new EngineServer(5004);
    p_filesystem = new QDir("/");

    qDebug() << p_filesystem->entryList(QDir::AllEntries).join("\n");

    p_engineServer->getSignalFactoryPtr()->declareSignalIn("filesAndDirsRequest");
    p_engineServer->getSignalFactoryPtr()->declareSignalOut("filesAndDirsReply");

    p_engineServer->connect("filesAndDirsRequest", this, EXECUTOR(replyFilesAndDirs));
}

FileBrowserEngine::~FileBrowserEngine(){
    delete p_engineServer;
    delete p_filesystem;
}


void FileBrowserEngine::replyFilesAndDirs(const Parameters& params){
    SignalOut signal = p_engineServer->signalOut("filesAndDirsReply");
    QString path = params.get("path").toString();
    p_filesystem->setPath(path);

    signal.addParameter("files", p_filesystem->entryList(QDir::Files).join("\n"));
    signal.addParameter("dirs", p_filesystem->entryList(QDir::Dirs | QDir::NoDot).join("\n"));

    p_engineServer->emitSignal(signal);
}
